const path = require("path");
const webpack = require("webpack");
const CommonsChunkPlugin = new webpack.optimize.CommonsChunkPlugin("common");
const htmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {

    entry: {
        "app": "./js/app.js",
        vendor: ['angular','jquery']

    },
    output: {
        path: path.resolve(__dirname, "./dist"),
        filename: "[name].bundle.js"
    },
    resolve: {
        alias: {
            "utils$": path.resolve(__dirname, "./js/lib/util.js"),
            "layer-mobile$":path.resolve(__dirname, "./js/lib/layer_mobile/layer.js"),
            "angular$":path.resolve(__dirname, "./js/lib/angular.js"),
            "angular-ui-router$":path.resolve(__dirname, "./js/lib/angular-ui-router.min.js"),
            "Lushu$":path.resolve(__dirname, "./js/lib/map/LuShu_min.js"),

        },
        extensions: [".js", ".json"],
        modules: [path.resolve(__dirname, "lib"), "node_modules"]
    },
    devServer:{
        // 服务器的根目录 Tell the server where to serve content from
        // https://webpack.js.org/configuration/dev-server/#devserver-contentbase
        contentBase: "./",
        // 自动打开浏览器
        open:true,
        // 端口号
        port:8888,

        // --------------- 1 热更新 -----------------
        hot: true,
        historyApiFallback: true
    },
    module: {
        rules: [{
            test: /\.js$/,
            exclude: /(node_modules|bower_components)/,
            loader: 'babel-loader',
            options: {
                presets: ['es2015']
            },

        },
            {
                test: /\.less|\.css$/,
                use: [{
                    loader: "style-loader" // creates style nodes from JS strings
                }, {
                    loader: "css-loader" // translates CSS into CommonJS
                }, {
                    loader: "less-loader" // compiles Less to CSS
                }]
            },
            {
                test: /\.(jpg|png|gif|jpeg)$/,
                use: [
                    {
                        loader: "url-loader",
                        options: {
                            limit: 8192
                        }
                    }
                ]
            }
        ]
    },
    plugins: [
        new webpack.HotModuleReplacementPlugin(),
        new webpack.optimize.UglifyJsPlugin(),
        // CommonsChunkPlugin
        new webpack.optimize.CommonsChunkPlugin({
            name: "vendor",
            minChunks: Infinity
        }),
        new htmlWebpackPlugin({
            // 模板页面路径
            template: path.join(__dirname, './index.html'),
            // 在内存中生成页面路径，默认值为：index.html
            filename: 'index.html'
        }),
    ]
};
